<?php

namespace App\Services\Pay\GuoTonXinYi;

use App\Services\Pay\ResultVo;
use Illuminate\Support\Facades\Log;

/**
 * 商户号配置接口
 */
class Mch
{
    public static function queryActSubDetail(string $mchNo, array $config): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::queryActSubDetail;
            $reqData = [
                "agetId" => $config["orgId"],
                "custId" => $mchNo,
                "page" => 1,
                "size" => 100,
                "timeStamp" => date("YmdHis", time()),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqData, $config["key"]);
            $reqData["sign"] = $sign;
            $resultArray = Config::post($url, $reqData);
            Log::channel("quota")->info("queryActSubDetail", [
                "reqData" => $reqData,
                "resData" => $resultArray
            ]);
            if (empty($resultArray) || !isset($resultArray["code"])) {
                throw new \RuntimeException("响应异常");
            }
            $resultVo = ResultVo::code("ok")::data($resultArray["data"] ?? [])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("failed")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }


    //业务开通查询
    public static function openBusinessQuery(): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::OpenBusinessQuery;
            //参数拼接
            $reqData = [
                "agetId" => Config::ORG_ID,
                "timestamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqData);
            $reqData["sign"] = $sign;
            //发送请求
            $resultArray = Config::post($url, $reqData);
            /**
             * {
             * "code":"",
             * "msg":"",
             * "data":Θ[
             * Θ{
             * "paramId":"“商户服务化配置信息配置”接口会用到",
             * "paramName":"“商户服务化配置信息配置”接口会用到"
             * }
             * ]
             * }
             */
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }


    //虚拟机构关联商户  000000 表示成功
    public static function connectCust(): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::connectCust;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //商户服务化配置信息配置
    public static function custConfigConfig(string $wxAppid, string $wxSecret, array $params): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::connectCust;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "paramId" => "", //开通业务中的参数
                "paramName" => "",
                "wxAppid" => $wxAppid,
                "wxSecret" => $wxSecret,
                "smallAppid" => $params["smallAppid"] ?? "",
                "smallSecret" => $params["smallSecret"] ?? "",
                "codeFrontUrl" => $params["codeFrontUrl"] ?? "",
                "wxQdh" => $params["wxQdh"] ?? "",
                "mAppid" => $params["mAppid"] ?? "",
                "zfbAppid" => $params["zfbAppid"] ?? "",
                "authPath1" => $params["authPath1"] ?? "",
                "authPath2" => $params["authPath2"] ?? "",
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //微信APPID配置
    public static function setAppidConfig(string $appId, string $appSecret, string $wxSubMercid, array $params): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::appidConfig;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "appid" => $appId,
                "appsecret" => $appSecret,
                "payType" => "01",//01公众号、02 小程序
                "payWay" => "05", //01银联微信、05银联ATQ微信
                "wxSubMercid" => $wxSubMercid,
                "agetAppid" => $params["agetAppid"] ?? "",
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //配置微信支付目录
    public static function setWxPayConfig(string $wxSubMercid, string $agetAppid, string $authPath): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::wxPayConfig;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "wxSubMercid" => $wxSubMercid,
                "agetAppid" => $agetAppid,
                "authPath" => $authPath,
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //查询商户微信开发配置
    public static function getCustWxAppidConfig(string $wxSubMercid): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::custWxAppidConfig;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "wxSubMercid" => $wxSubMercid,
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //公众号查询
    public static function getWxInfo(): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::getWxInfo;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //获取银联用户标识
    public static function getPaypalTag(): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::getPaypalTag;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    //获取银联用户标识
    public static function getOpenid(string $wxappid, string $code): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::openid;
            $reqDate = [
                "agetId" => Config::ORG_ID,
                "custId" => Config::MCH_NO,
                "timeStamp" => (string)time(),
                "version" => Config::VERSION,
                "wxappid" => $wxappid,
                "data" => $code
            ];
            $sign = Config::getSign($reqDate);
            $reqDate["sign"] = $sign;
            $resultArray = Config::post($url, $reqDate);
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("error")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

}